package xyz.ag.Servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import xyz.ag.Beans.Book;
import xyz.ag.Service.BookServiceInterface;
import xyz.ag.Service.impl.BookServiceInterfaceImpl;
/**
 * 图书相关的业务处理的Servlet
 * @author User
 *
 */
@WebServlet("*.book")
public class BookServlet extends HttpServlet {
	
	/**
	 * 常用变量定义
	 * 
	 */
	BookServiceInterface BookSI = new BookServiceInterfaceImpl();
	
	
	/**
     * 覆盖service方法 来实现对前端数据的接受
     */
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method0  = req.getServletPath();//获取路径
		String method = "";
		try {//路径处理
			 method = method0.substring(1,method0.length()-5);
		} catch (Exception  a) {
			 method = method0.substring(1,method0.length()-5);
		}
		if("indexbookloda".equals(method)) BookLoad(req,resp);//首页推荐图书信息加载接口
		if("indexbooknumber".equals(method)) indexbooknumber(req,resp);//首页推荐总计数
		if("initshowlist".equals(method)) initshowlist(req,resp);//查询全部的图书信息/或按照类型查询
		if("initshowlistnumber".equals(method)) initshowlistnumber(req,resp);//查询全部的图书信息/或按照类型查询
		if("shouBookByBookName".equals(method)) shouBookByBookName(req,resp);//查询全部的图书信息  根据书名来查询
		if("point".equals(method)) point(req,resp);//查询全部的图书信息  根据书名来查询
		
	}

	/**
	 * 图书展示锚点接口 只用来重定向 没有任何功能
	 * @param req
	 * @param resp
	 */
	private void point(HttpServletRequest req, HttpServletResponse resp) {
		try {
			req.getRequestDispatcher("showOneBook.jsp").forward(req, resp);
//			resp.sendRedirect("showOneBook.jsp");
		} catch (IOException | ServletException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
	}

	/**
	 * 前端获取展示页面的AJAX函数 这里用来获取展示页面的get提供的书名 并返回一个封装好的对象供前段展示
	 * ps jsp的效率真的是差的不行
	 * @param req
	 * @param resp
	 */
	private void shouBookByBookName(HttpServletRequest req, HttpServletResponse resp) {
		resp.setContentType("text/json;charset=utf-8");
		
		String bookname=req.getParameter("bookname");
		Book book = BookSI.shouBookByBookName(bookname);
		String s = JSON.toJSONString(book).toString();
		try {
			resp.getWriter().write(s);
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
	}

	/**
	 * 查询图书的总目录数 为了分页做准备
	 * @param req
	 * @param resp
	 */
	private void initshowlistnumber(HttpServletRequest req, HttpServletResponse resp) {
		String s = req.getParameter("bookification");
		resp.setContentType("text/json;charset=utf-8");
		
	    Integer booknumber = BookSI.initshowlistnumber(s);//根据条件获得图书1
	    System.out.println(booknumber);
	    try {
			resp.getWriter().write(String.valueOf(booknumber));
		} catch (IOException e) {
			
			e.printStackTrace();
		}
	}

	/**
	 * 查询所有的图示列表 根据前端返回的类型要求来展示
	 * 
	 * @param req
	 * @param resp
	 */
	private void initshowlist(HttpServletRequest req, HttpServletResponse resp) {
		resp.setContentType("text/json;charset=utf-8");
		String s = req.getParameter("bookification");
		String page = req.getParameter("page");
		String number = req.getParameter("number");
		
	
		ArrayList<Book> booklist  = BookSI.getBookListByCondition(page,number,s);//根据条件获得图书1
		String jsons= JSON.toJSON(booklist).toString();//构造JSno字符串
		try {
			String newstring=new String(jsons.getBytes("ISO-8859-1"),"utf-8");
			resp.getWriter().write("{\"start\":\""+booklist.size()+"\",\"jsons\":"+newstring+"}");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	private void indexbooknumber(HttpServletRequest req, HttpServletResponse resp) {
		resp.setContentType("text/json;charset=utf-8");
		Integer s = BookSI.getrecommendedListNmuber();
//		/System.out.println("请求OK="+s);
		try {
			
			resp.getWriter().write("{\"a\":\""+String.valueOf(s)+"\"}");
		} catch (IOException e) {
			System.err.println("总数获取异常");
			e.printStackTrace();
		}
	}

	
	/**
	 * 这个接口应该返回一个json字符串
	 * 它负责携带至多4字符串数组个参数进行回调函数的响应
	 * 来实现分页展示的效果
	 * @param req
	 * @param resp
	 */
	private void BookLoad(HttpServletRequest req, HttpServletResponse resp) {
		resp.setContentType("text/json;charset=utf-8");
		String page = req.getParameter("page");
		String number = req.getParameter("number");
		ArrayList<Book> listbook = BookSI.getBookLimitpageList(page,number);
		
		String jsons= JSON.toJSON(listbook).toString();//构造JSno字符串
	
		try {
			resp.getWriter().write("{\"start\":\""+listbook.size()+"\",\"jsons\":"+jsons+"}");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}














